﻿//-----------------------------------------------------------------------
// <copyright file="ShowSendPrivateMessageCommand.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 
namespace ChatBox.UI.Windows.SendPrivateMessage
{
    using System;
    using System.Windows.Documents;

    using ChatBox.DomainObjects.XmlFeedObjects;
    using ChatBox.Interfaces;
    using ChatBox.UI.Commands;
    using ChatBox.Utility;

    /// <summary>
    /// Command to show the SendPrivateMessage window
    /// </summary>
    public class ShowSendPrivateMessageCommand : GenericCommandBase<ShowPrivateMessageParameters>
    {
        /// <summary>
        /// the source of UI settings
        /// </summary>
        private readonly IUISettingsRepository uiSettingsRepository;

        /// <summary>
        /// Initializes a new instance of the ShowSendPrivateMessageCommand class
        /// </summary>
        /// <param name="uiSettingsRepository">the source of UI settings</param>
        public ShowSendPrivateMessageCommand(IUISettingsRepository uiSettingsRepository)
        {
            this.uiSettingsRepository = uiSettingsRepository;
        }

        /// <summary>
        /// Show the SendPrivateMessage window
        /// </summary>
        /// <param name="value">the data to show</param>
        public override void ExecuteOnValue(ShowPrivateMessageParameters value)
        {
            SendPrivateMessageViewModel viewModel = Locator.Resolve<SendPrivateMessageViewModel>();
            this.Populate(value, viewModel);

            SendPrivateMessageWindow sendWindow = new SendPrivateMessageWindow();
            sendWindow.DataContext = viewModel;
            viewModel.Window = sendWindow;

            sendWindow.Loaded += (s, e) => sendWindow.CenterInWindow(value.ParentWindow);
            sendWindow.Show();
        }

        /// <summary>
        /// Populate the view model from the data
        /// </summary>
        /// <param name="model">the values to put in</param>
        /// <param name="viewModel">the view model</param>
        private void Populate(ShowPrivateMessageParameters model, SendPrivateMessageViewModel viewModel)
        {
            viewModel.SpellCheckIsEnabled = this.uiSettingsRepository.ReadSettings().UseBuiltInSpellchecker;

            viewModel.MessageRecipient = model.MessageRecipient;
            viewModel.OperationCompletedCommand = model.OperationCompletedCommand;

            PopulateTitle(model, viewModel);
            PopulateMessageTo(model, viewModel);
        }
        
        /// <summary>
        /// Populate the title field
        /// </summary>
        /// <param name="value">the values to put in</param>
        /// <param name="viewModel">the view model</param>
        private static void PopulateTitle(ShowPrivateMessageParameters value, SendPrivateMessageViewModel viewModel)
        {
            if (value.MessageRecipient.GroupType == GroupType.UserGroup)
            {
                viewModel.TitleText = "Message to " + value.MessageRecipient.Text;
            }
            else
            {
                viewModel.TitleText = "Private message to " + value.MessageRecipient.Text;
            }
        }

        /// <summary>
        /// Populate the "message to" field
        /// </summary>
        /// <param name="value">the values to put in</param>
        /// <param name="viewModel">the view model</param>
        private static void PopulateMessageTo(ShowPrivateMessageParameters value, SendPrivateMessageViewModel viewModel)
        {
            Span messageToContent = new Span();
            viewModel.LabelContent = messageToContent;

            Hyperlink recipientLink = new Hyperlink(new Run(value.MessageRecipient.Text));
            string linkTarget = value.HyperlinkContext.SitePrefix + "/node/" + viewModel.MessageRecipient.NodeId;
            recipientLink.NavigateUri = new Uri(linkTarget);
            value.HyperlinkContext.SetContextOnHyperlink(recipientLink);

            if (viewModel.MessageRecipient.GroupType == GroupType.UserGroup)
            {
                messageToContent.Inlines.Add("Message to group ");
            }
            else
            {
                messageToContent.Inlines.Add("Message to ");
            }

            messageToContent.Inlines.Add(recipientLink);
        }
    }
}
